/*     */ package com.services.bo;
/*     */ 
/*     */ import java.io.PrintStream;
/*     */ import java.nio.charset.Charset;
/*     */ import java.security.MessageDigest;
/*     */ import java.security.NoSuchAlgorithmException;
/*     */ 
/*     */ public class Validasi
/*     */ {
/*     */   private static final int PAD_LIMIT = 8192;
/*   8 */   private static String clearKey = "KlikPayDevLegend";
/*   9 */   private static String clickPayCode = "01LEGE007";
/*     */   private String firstValue;
/*     */   private Long secondValue;
/*     */   private String transactionNo;
/*     */   private String currency;
/*     */   private String transTime;
/*     */ 
/*     */   public Validasi(String transactionNo, String currency, String transTime, long transAmount)
/*     */   {
/*  19 */     System.out.println("TransNo: " + transactionNo + ",currency: " + currency + ",amount:" + transAmount);
/*  20 */     this.transactionNo = transactionNo;
/*  21 */     this.currency = currency;
/*  22 */     this.transTime = transTime;
/*  23 */     this.firstValue = (clickPayCode + transactionNo + currency + generateKeyID());
/*  24 */     String tmpTime = transTime.substring(0, 8);
/*  25 */     this.secondValue = Long.valueOf(Long.parseLong(tmpTime) + transAmount);
/*     */   }
/*     */ 
/*     */   public Validasi(String transactionNo, String currency, String transTime)
/*     */   {
/*  30 */     System.out.println("TransNo: " + transactionNo + ",currency: " + currency + ",date:" + transTime);
/*  31 */     this.transactionNo = transactionNo;
/*  32 */     this.currency = currency;
/*     */   }
/*     */ 
/*     */   private long hashMe(String value) {
/*  36 */     long minVal = -2147483648L;
/*  37 */     long maxVal = 2147483647L;
/*  38 */     long hash = 0L;
/*  39 */     Charset characterSet = Charset.forName("US-ASCII");
/*     */ 
/*  41 */     byte[] arrValue = value.getBytes(characterSet);
/*  42 */     for (int i = 0; i < value.length(); i++)
/*     */     {
/*  44 */       hash = hash * 31L + arrValue[i];
/*     */     }
/*     */ 
/*  47 */     if (hash > maxVal)
/*  48 */       hash = hash + minVal - maxVal - 1L;
/*  49 */     else if (hash < minVal)
/*  50 */       hash = hash + maxVal - minVal + 1L;
/*  51 */     return hash;
/*     */   }
/*     */ 
/*     */   public String generateKeyID()
/*     */   {
/*  56 */     char[] chars = clearKey.toCharArray();
/*  57 */     StringBuffer hex = new StringBuffer();
/*  58 */     for (int i = 0; i < chars.length; i++) {
/*  59 */       hex.append(Integer.toHexString(chars[i]));
/*     */     }
/*  61 */     return hex.toString().toUpperCase();
/*     */   }
/*     */ 
/*     */   public String generateSignature()
/*     */   {
/*  66 */     long hash1 = hashMe(this.firstValue);
/*  67 */     long hash2 = hashMe(String.valueOf(this.secondValue));
/*  68 */     System.out.println("hash1: " + hash1 + "  :  " + "hash2: " + hash2);
/*  69 */     return String.valueOf(hash1 + hash2);
/*     */   }
/*     */ 
/*     */   private boolean isEmpty(String str) {
/*  73 */     return (str == null) || (str.length() == 0);
/*     */   }
/*     */ 
/*     */   private String rightPad(String str, int size, char padChar) {
/*  77 */     if (str == null) {
/*  78 */       return null;
/*     */     }
/*  80 */     int pads = size - str.length();
/*  81 */     if (pads <= 0) {
/*  82 */       return str;
/*     */     }
/*  84 */     if (pads > 8192) {
/*  85 */       return rightPad(str, size, String.valueOf(padChar));
/*     */     }
/*  87 */     return str.concat(padding(pads, padChar));
/*     */   }
/*     */ 
/*     */   private String rightPad(String str, int size, String padStr) {
/*  91 */     if (str == null) {
/*  92 */       return null;
/*     */     }
/*  94 */     if (isEmpty(padStr)) {
/*  95 */       padStr = " ";
/*     */     }
/*  97 */     int padLen = padStr.length();
/*  98 */     int strLen = str.length();
/*  99 */     int pads = size - strLen;
/* 100 */     if (pads <= 0) {
/* 101 */       return str;
/*     */     }
/* 103 */     if ((padLen == 1) && (pads <= 8192)) {
/* 104 */       return rightPad(str, size, padStr.charAt(0));
/*     */     }
/*     */ 
/* 107 */     if (pads == padLen)
/* 108 */       return str.concat(padStr);
/* 109 */     if (pads < padLen) {
/* 110 */       return str.concat(padStr.substring(0, pads));
/*     */     }
/* 112 */     char[] padding = new char[pads];
/* 113 */     char[] padChars = padStr.toCharArray();
/* 114 */     for (int i = 0; i < pads; i++) {
/* 115 */       padding[i] = padChars[(i % padLen)];
/*     */     }
/* 117 */     return str.concat(new String(padding));
/*     */   }
/*     */ 
/*     */   private static String padding(int repeat, char padChar) throws IndexOutOfBoundsException
/*     */   {
/* 122 */     if (repeat < 0) {
/* 123 */       throw new IndexOutOfBoundsException("Cannot pad a negative amount: " + repeat);
/*     */     }
/* 125 */     char[] buf = new char[repeat];
/* 126 */     for (int i = 0; i < buf.length; i++) {
/* 127 */       buf[i] = padChar;
/*     */     }
/* 129 */     return new String(buf);
/*     */   }
/*     */ 
/*     */   public String generateAuthKey() throws NoSuchAlgorithmException {
/* 133 */     String paddKlickPayCode = rightPad(clickPayCode, 10, "0");
/* 134 */     String paddTransNo = rightPad(this.transactionNo, 18, "A");
/* 135 */     String paddCurrency = rightPad(this.currency, 5, "1");
/* 136 */     String paddTransTime = rightPad(this.transTime, 19, "C");
/* 137 */     String keyID = rightPad(generateKeyID(), 32, "E");
/* 138 */     String value = paddKlickPayCode + paddTransNo + paddCurrency + paddTransTime + keyID;
/* 139 */     System.out.println("pad: " + value);
/* 140 */     String md5Value = MD5(value);
/* 141 */     System.out.println("md5Value: " + md5Value.toUpperCase());
/*     */     try
/*     */     {
/* 144 */       System.out.println("KeyID:" + keyID);
/* 145 */       DESedeEncryption enc = new DESedeEncryption(keyID);
/* 146 */       String encrypt = enc.encrypt(md5Value.toUpperCase());
/* 147 */       System.out.println("AuthKey:" + encrypt);
/* 148 */       return encrypt;
/*     */     }
/*     */     catch (Exception e) {
/* 151 */       e.printStackTrace();
/*     */     }
/*     */ 
/* 154 */     return "";
/*     */   }
/*     */ 
/*     */   private String MD5(String value)
/*     */   {
/*     */     try
/*     */     {
/* 162 */       MessageDigest md = MessageDigest.getInstance("MD5");
/* 163 */       md.update(value.getBytes());
/* 164 */       byte[] byteData = md.digest();
/*     */ 
/* 166 */       StringBuffer sb = new StringBuffer();
/* 167 */       for (int i = 0; i < byteData.length; i++) {
/* 168 */         sb.append(Integer.toString((byteData[i] & 0xFF) + 256, 16).substring(1));
/*     */       }
/* 170 */       return sb.toString();
/*     */     }
/*     */     catch (NoSuchAlgorithmException e) {
/* 173 */       e.printStackTrace();
/*     */     }
/* 175 */     return "";
/*     */   }
/*     */ }

/* Location:           F:\BCA.jar
 * Qualified Name:     com.services.bo.Validasi
 * JD-Core Version:    0.6.0
 */